package cn.ycxy.lbl.posts.consumer;

import cn.ycxy.lbl.pojo.posts.constant.DiscussPostsConstant;
import cn.ycxy.lbl.pojo.posts.domain.LblCsDiscussPost;
import cn.ycxy.lbl.pojo.search.domain.CommonDiscussPosts;
import cn.ycxy.lbl.pojo.search.domain.DealDiscussPosts;
import cn.ycxy.lbl.search.service.ISearchCommonService;
import cn.ycxy.lbl.search.service.ISearchDealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class PostsQueueConsumer implements DiscussPostsConstant {

    @DubboReference(timeout = 5000)
    private ISearchCommonService dubboSearchCommonService;
    @DubboReference(timeout = 5000)
    private ISearchDealService dubboSearchDealService;
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
    // 监听添加帖子
    @RabbitListener(bindings = @QueueBinding( // 队列绑定交换机
            value = @Queue(CS_POSTS_ADD_QUEUE), // 创建队列
            exchange = @Exchange(value = CS_POSTS_EX, type = ExchangeTypes.DIRECT), // 创建交换机
            key = CS_POSTS_ADD_RK // 路由分组
    ))
    public void listenAddPostsQueue(LblCsDiscussPost discussPost) {
        Long dateTimeNumber = Long.parseLong(formatter.format(LocalDateTime.now()));
        //此处调用搜索服务, 将帖子添加到es中
        log.info("接收到帖子新增消息, 新增帖子类型为: {}帖子, 开始使用dubbo远程调用Es服务...", getTypeStr(discussPost.getType()));
        if (discussPost.getType() == POSTS_DEAL_TYPE){
            DealDiscussPosts dealDiscussPosts = new DealDiscussPosts();
            BeanUtils.copyProperties(discussPost, dealDiscussPosts);
            dealDiscussPosts.setCreateTime(dateTimeNumber);
            dealDiscussPosts.setUpdateTime(dateTimeNumber);
            dubboSearchDealService.saveDiscussPost(dealDiscussPosts);
        }else {
            CommonDiscussPosts commonDiscussPosts = new CommonDiscussPosts();
            BeanUtils.copyProperties(discussPost, commonDiscussPosts);
            commonDiscussPosts.setCreateTime(dateTimeNumber);
            commonDiscussPosts.setUpdateTime(dateTimeNumber);
            dubboSearchCommonService.saveDiscussPost(commonDiscussPosts);
        }
        log.info("使用dubbo远程调用Es服务保存帖子操作成功! postId为: {}", discussPost.getId());
    }

    // 监听删除帖子
    @RabbitListener(bindings = @QueueBinding( // 队列绑定交换机
            value = @Queue(CS_POSTS_DEL_QUEUE), // 创建队列
            exchange = @Exchange(value = CS_POSTS_EX, type = ExchangeTypes.DIRECT), // 创建交换机
            key = CS_POSTS_DEL_RK // 路由分组,
    ))
    public void listenDirectQueue2(LblCsDiscussPost discussPost) {
        //此处调用搜索服务, 将帖子从es中删除
        log.info("接收到帖子移除消息, 移除帖子类型为: {}, id为: {}, 开始使用dubbo远程调用Es服务...",
                getTypeStr(discussPost.getType()), discussPost.getId());
        if (discussPost.getType() == POSTS_DEAL_TYPE){
            dubboSearchDealService.deleteDiscussPostById(discussPost.getId());
        }else {
            dubboSearchCommonService.deleteDiscussPostById(discussPost.getId());
        }
        log.info("使用dubbo远程调用Es服务删除帖子操作成功! postId为: {}", discussPost.getId());
    }

    public String getTypeStr(int type){
        return type == POSTS_DEAL_TYPE ? "二手交易" : "失物招领、拼车";
    }
    
}
